package com.yiwei.zxp.view;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.AddressLeftAdapter;
import com.yiwei.zxp.adapter.AddressRightAdapter;
import com.yiwei.zxp.adapter.AreaRightAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.IdBean;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class SelectAreaDilaog extends Dialog implements AddressLeftAdapter.OnChooseListener, AddressRightAdapter.OnChooseListener, AreaRightAdapter.OnChooseListener {


    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.line_province)
    View lineProvince;
    @BindView(R.id.line_city)
    View lineCity;
    @BindView(R.id.list_province)
    RecyclerView listProvince;
    @BindView(R.id.list_city)
    RecyclerView listCity;
    @BindView(R.id.loading)
    CircularProgress loading;
    @BindView(R.id.btn_province)
    TextView btnProvince;
    @BindView(R.id.btn_city)
    TextView btnCity;
    @BindView(R.id.btn_area)
    TextView btnArea;
    @BindView(R.id.line_area)
    View lineArea;
    @BindView(R.id.list_area)
    RecyclerView listArea;
    private Context context;
    private AddressLeftAdapter addressLeftAdapter;
    private AddressRightAdapter addressRightAdapter;
    private AreaRightAdapter areaRightAdapter;
    String provinceId;
    String provinceName;
    String cityId;
    String cityName;
    String areaId;
    String areaName;
    private boolean isHasProvinceData = false;

    public SelectAreaDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public SelectAreaDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_area_select);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        mWindow.setAttributes(lp);

        addressLeftAdapter = new AddressLeftAdapter(context);
        addressLeftAdapter.setOnChooseListener(this);
        addressRightAdapter = new AddressRightAdapter(context);
        addressRightAdapter.setOnChooseListener(this);

        areaRightAdapter = new AreaRightAdapter(context);
        areaRightAdapter.setOnChooseListener(this);
        listProvince.setAdapter(addressLeftAdapter);
        listCity.setAdapter(addressRightAdapter);
        listArea.setAdapter(areaRightAdapter);

    }

    @Override
    public void show() {
        super.show();
        if (!isHasProvinceData)
            getProvince();
    }

    public void getProvince() {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        Api.api_service.getProvince().compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    isHasProvinceData = true;
                    addressLeftAdapter.update(result.datas);
                });
    }

    public void getCity(String id) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        IdBean bean = new IdBean();
        bean.id = id;
        Api.api_service.getCity(bean).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    addressRightAdapter.update(result.datas);
                    lineProvince.setVisibility(View.GONE);
                    lineCity.setVisibility(View.VISIBLE);
                    lineArea.setVisibility(View.GONE);
                });
    }

    public void getArea(String id) {
        if (!Network.checkNetwork(context)) {
            ToastUtil.show(context, "网络连接失败");
            return;
        }
        loading.setVisibility(View.VISIBLE);
        IdBean bean = new IdBean();
        bean.id = id;
        Api.api_service.getArea(bean).compose(RxLifeUtil.checkOn(context)).
                doFinally(() -> {
                    try {
                        loading.setVisibility(View.GONE);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    areaRightAdapter.update(result.datas);
                    lineProvince.setVisibility(View.GONE);
                    lineCity.setVisibility(View.GONE);
                    lineArea.setVisibility(View.VISIBLE);
                    if (result.datas == null || result.datas.size() == 0) {
                        if (onChooseListener != null) {
                            onChooseListener.onAddressSelect(provinceId, provinceName, cityId, cityName, "", "");
                        }
                        dismiss();
                    }
                });
    }

    @Override
    public void onCitySelect(String id, String name) {
        this.cityId = id;
        this.cityName = name;
        getArea(id);

    }

    @Override
    public void onProvinceSelect(String id, String name) {
        this.provinceId = id;
        this.provinceName = name;
        getCity(id);
    }

    private OnChooseListener onChooseListener;

    public SelectAreaDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    @OnClick({R.id.btn_close, R.id.btn_province, R.id.btn_city})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.btn_province:
                break;
            case R.id.btn_city:
                break;
        }
    }

    @Override
    public void onAreaSelect(String id, String name) {
        areaId = id;
        areaName = name;
        if (onChooseListener != null) {
            onChooseListener.onAddressSelect(provinceId, provinceName, cityId, cityName, areaId, areaName);
        }
        dismiss();
    }

    public interface OnChooseListener {
        void onAddressSelect(String provinceId, String provinceName, String cityId, String cityName, String areaId, String areaName);
    }
}
